import 'package:http/http.dart' as http;
import 'package:http/http.dart';

import 'dart:convert';

// Future对象表示异步操作的结果，我们通常通过then（）来处理返回的结果
// async用于标明函数是一个异步函数，其返回值类型是Future类型
// await用来等待耗时操作的返回结果，这个操作会阻塞到后面的代码
class ResustHttp{

  static String apiurl = "http://a.itying.com/api/productlist";
  static String apiurllogin = "http://a.itying.com/api/dologin";
  static String newListString  = "http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=";
  static String detaisApi = "http://www.phonegap100.com/appapi.php?a=getPortalArticle&aid=";
  static Future<dynamic> getListData () async {
    Response result = await http.get(apiurl);
    if(result.statusCode == 200){
      //return json.decode(result.body);
      return json.decode(result.body);
    }else{
      print("失败 ${result.statusCode}");
    }
  }
  static Future<dynamic> getNewListData (int pageindex) async {
    String apiurl = newListString + "$pageindex";
    Response result = await http.get(apiurl);
    if(result.statusCode == 200){
      //return json.decode(result.body);
      return json.decode(result.body);
    }else{
      print("失败 ${result.statusCode}");
    }
  }
  static Future<dynamic> getNewDetails (code) async {
    String apiurl = detaisApi +"$code";
    Response result = await http.get(apiurl);
    if(result.statusCode == 200){
      //return json.decode(result.body);
      return json.decode(result.body);
    }else{
      print("失败 ${result.statusCode}");
    }
  }
}
